package com.xing.msmServer.utils;

import com.xing.commonutils.R;
import org.aspectj.weaver.ast.Var;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * 获取随机数的工具类
 *
 * @author: xingfuhao
 * @create: 2022-03-26 09:41
 */
public class RandomUtil {

    private static final Random random =new Random();

    private static final DecimalFormat fourdf = new DecimalFormat("0000");

    private static final DecimalFormat sixdf=new DecimalFormat("000000");



    //返回字符串，格式化的四位字符串
    public static String getFourdf() {
        return fourdf.format(random.nextInt(10000));
    }

    //返回字符串，格式化的六位字符串
    public static String getSixdf() {
        return fourdf.format(random.nextInt(1000000));
    }

    /**
     * 给定数组，抽取n个数据
     * @return
     */
    public static ArrayList getRandom(List list, int n) {

        Random random = new Random();
        HashMap<Object, Object> hashMap = new HashMap<>();

        //生成随机数字并存入hashmap中
        for (int i = 0; i < list.size(); i++) {
            int number = random.nextInt(100)+1;
            hashMap.put(number, 1);
        }
        //从HashMap导入数据
        Object[] robjs = hashMap.values().toArray();
        ArrayList r = new ArrayList<>();

        //遍历数组并打印数据
        for (int i = 0; i < n; i++) {
            r.add(list.get((int) robjs[i]));
            System.out.println(list.get((int) robjs[i])+"\t");
        }
        System.out.println("\n");
        return r;
    }

}
